package application.gui.clientWindow;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PrinterException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.LinkedList;

import javax.print.PrintException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;

import com.itextpdf.text.DocumentException;

import application.CommunicationModule.Communication;
import application.LoginAndAdministrationModule.PersonData;
import application.PrintingModule.Printing;
import application.client.data.Client;
import application.parcelModule.Parcel;

public class ShowDetails extends JFrame{
	public ShowDetails(ClientHomePage parent, Communication c, PersonData p, Parcel parcel){
		this.parent = parent;
		this.communication = c;
		this.personData = p;
		this.parcel = parcel;
		setSize(480,320);
		setLayout(null);
		setResizable(false);
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		initTable();
		initComponents();
	}
	
	public JTable getTable() {
		return table;
	}

	public void setTable(JTable table) {
		this.table = table;
	}

	private void initComponents(){
		buttonPanel = new JPanel(null);
		buttonPanel.setSize(480,50);
		close = new JButton("Zamknij");
		buttonPanel.add(close);
		close.setBounds(300, 0, 100, 40);
		printLabel = new JButton("Drukuj etykietę");
		buttonPanel.add(printLabel);
		printLabel.setBounds(0, 0, 150, 40);
		add(buttonPanel,BorderLayout.SOUTH);
		buttonPanel.setBounds(0, 200, 480, 50);
		close.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				parent.setVisible(true);
				dispose();
			}
		});
		printLabel.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				Printing print = new Printing();
				try {
					print.generateLabel("out\\etykieta.pdf", communication.getClientsByField("id", String.valueOf(parcel.getRecipient())).get(0), parcel.getParcelID());
					try {
						print.printDocument("out\\etykieta.pdf");
					} catch (PrintException e1) {
						// TODO Auto-generated catch block
					} catch (PrinterException e1) {
						// TODO Auto-generated catch block
					}
				} catch (DocumentException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});
		changeRecipient = new JButton("Zmień adresata");
		buttonPanel.add(changeRecipient);
		changeRecipient.setBounds(150, 0, 150, 40);
		if (parcel.getStatus()!=1 && parcel.getStatus()!=2){
			changeRecipient.setVisible(false);
		}else{
			changeRecipient.setVisible(true);
		}
		changeRecipient.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				parent.registerMsg(2);
				try {
					parent.getNewParcelPanel().editParcelMode(communication.getClientsByField("id", String.valueOf(parcel.getRecipient())).get(0), parcel);
					dispose();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
	}
	
	public void initTable(){
		LinkedList<String[]> list = new LinkedList<String[]>();
		Client c;
		try {
			c = communication.getClientsByField("id", String.valueOf(parcel.getRecipient())).get(0);
			String[] row = {"ID paczki",String.valueOf(parcel.getParcelID())};
			list.add(row);
			row = new String[]{"Imię i nazwisko nadawcy",personData.getName()+" "+personData.getSurname()};
			list.add(row);
			row = new String[]{"Data nadania",parcel.getDate()};
			list.add(row);
			row = new String[]{"Wymiary",String.valueOf(parcel.getWidth())+"x"+String.valueOf(parcel.getHeight())+"x"+String.valueOf(parcel.getLength())+" - "+String.valueOf(parcel.getMass())+"dag"};
			list.add(row);
			row = new String[]{"Cena",String.valueOf(parcel.getPrice())};
			list.add(row);
			row = new String[]{"Imię i nazwisko odbiorcy",c.getFirstName()+" "+c.getLastName()};
			list.add(row);
			row = new String[]{"Adres",c.getAddress().getStreet()+", "+c.getAddress().getFlatNumber()+"/ "+c.getAddress().getHouseNumber()};
			list.add(row);
			row = new String[]{"Kod pocztowy",c.getAddress().getPostCode()};
			list.add(row);
			row = new String[]{"Miejscowość",c.getAddress().getCity()};
			list.add(row);
			row = new String[]{"Status dostarczenia",communication.getParcelStatus(parcel.getParcelID())};
			list.add(row);
			setData(list.toArray(new String[2][list.size()]));
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		table = new JTable(getData(), names);
		add(table);
		table.setBounds(0, 0, 480, 200);
		
		
	}
	
	private ClientHomePage parent;
	private Communication communication;
	private PersonData personData;
	private Parcel parcel;
	private JTable table;
	private String [] names={"",""};
	private String [][] data;
	private JButton close;
	private JButton printLabel;
	private JButton changeRecipient;
	private JPanel buttonPanel;
	public Parcel getParcel() {
		return parcel;
	}

	public void setParcel(Parcel parcel) {
		this.parcel = parcel;
	}

	public JButton getPrintLabel() {
		return printLabel;
	}

	public void setPrintLabel(JButton printLabel) {
		this.printLabel = printLabel;
	}

	public JButton getChangeRecipient() {
		return changeRecipient;
	}

	public void setChangeRecipient(JButton changeRecipient) {
		this.changeRecipient = changeRecipient;
	}

	public JPanel getButtonPanel() {
		return buttonPanel;
	}

	public void setButtonPanel(JPanel buttonPanel) {
		this.buttonPanel = buttonPanel;
	}

	public Communication getCommunication() {
		return communication;
	}

	public String [][] getData() {
		return data;
	}

	public void setData(String [][] data) {
		this.data = data;
	}

	public String [] getNames() {
		return names;
	}

	public void setNames(String [] names) {
		this.names = names;
	}
}
